package models.vos.accounts {

	import models.vos.project.Project;
	import models.vos.project.ProjectMember;
	
	import mx.collections.ArrayCollection;
	
	[Bindable]
	public class User {
		
		public var id:Number;
		public var username:String;
		public var password:String;		
		
		public var confirmed:Boolean = true;
		public var active:Boolean = true;
		public var registeredDate:Date = new Date();
		public var contactInfo:ContactInfo = new ContactInfo();
		public var contactCards:ArrayCollection = new ArrayCollection();

		public function get email():String{
			if( this.contactInfo.emails != null && this.contactInfo.emails.length > 0 ){
				for each (var em:EmailAddress in this.contactInfo.emails){
					if (em.isMain) {return em.email};
				}
				return (this.contactInfo.emails.getItemAt(0) as EmailAddress).email;
			}
			return "";
		}
		
		public function set email(email:String):void{
			var isNewEmail:Boolean = true;
			if( this.contactInfo.emails == null ){ 
				this.contactInfo.emails = new ArrayCollection();
			};
			for each (var em:EmailAddress in this.contactInfo.emails){
				em.isMain = false;
				if( em.email == email ){
					isNewEmail = false;
					em.isMain = true;
				}
			}
			if( isNewEmail ){
				this.contactInfo.emails.addItem( new EmailAddress(email, "default", true) );
			}
			
		}
		
		public function User(username:String = "", password:String = "", email:String = "", id:Number = 0){
			
			this.username = username;
			this.password = password;
			this.contactInfo = new ContactInfo();
			this.contactInfo.emails.addItem(
				new EmailAddress(email, "default", true)
			);

		}
		public function addToProject(project:Project, isManager:Boolean = false, memberId:Number = 0):void{
			var pMember:ProjectMember = new ProjectMember( 
				this.username, this.password, this.email, this.id, memberId, isManager
			);
			if( project.members == null ) { project.members = new ArrayCollection() };
			project.members.addItem( pMember );
		}
		
	} // end class
} // end package